const formidable = require("formidable");
const path = require('path');
const { Face } = require('../../model/face');

//  设置APPID/AK/SK
const APP_ID = "24637433";
const API_KEY = "UEE1BaspBS7EHLD8RkWaqwe5";
const SECRET_KEY = "eWeklayC9xg4oPQMP8KRB455ICBDVpot";
//  调用服务接口
const client = new AipFaceClient(APP_ID, API_KEY, SECRET_KEY);

module.exports = (req, res) => {
    //  创建表单解析对象
    const form = new formidable.IncomingForm();
    //  配置人脸图片存放路径
    form.uploadDir = path.join(__dirname, '../', '../', 'public', 'uploads');
    //  保留图片后缀
    form.keepExtensions = true;
    //  解析表单
    form.parse(req, async(err, fields, files) => {
        if (err) {
            //  产生错误重新注册
            res.redirect('/home/faceRegister');
        } else {
            //  向人脸信息数据库中添加用户人脸信息
            await Face.create({
                imageURL: fields.imageURL.path.split('public')[1],
                imageType: fields.imageType,
                groupId: groupId,
                userId: userId
            });
        }
    });
}